<!-- 排班管理 -->
<template>
  <div class="dashboard-container transport-task customer-list-box">
    <div class="app-container">
      <el-card
        class="search-card-box"
        shadow="never"
      >
        <el-form
          ref="form"
          :model="searchFormData"
          label-width="80px"
        >
          <div class="filter-container">
            <el-row :gutter="60">
              <el-col :span="8">
                <el-form-item
                  label="员工账号:"
                  style="margin-bottom: 20px"
                >
                  <el-input
                    v-model="searchFormData.employeeNumber"
                    placeholder="请输入员工账号"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="员工姓名:"
                  style="margin-bottom: 20px"
                >
                  <el-input
                    v-model="searchFormData.name"
                    placeholder="请输入员工姓名"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="工作模式:"
                  style="margin-bottom: 20px"
                >
                  <el-select
                    v-model="searchFormData.workPatternId"
                    clearable
                    placeholder="请选择工作模式"
                    filterable
                  >
                    <el-option
                      v-for="item in workPatternList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="月份:"
                  style="margin-bottom: 0px"
                >
                  <el-date-picker
                    v-model="searchFormData.month"
                    type="month"
                    placeholder="请选择月份"
                    format="yyyy-MM"
                    value-format="yyyy-MM"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="所属机构:"
                  style="margin-bottom: 0px"
                >
                  <treeselect
                    v-model="searchFormData.agencyId"
                    :options="agencyOptions"
                    :normalizer="normalizer"
                    :searchable="true"
                    style="width: 100%"
                    placeholder="请选择所属机构"
                    no-options-text="暂无数据"
                    no-results-text="暂无数据"
                    @select="selectAgency"
                    @open="open"
                    @close="close"
                  />
                  <img
                    ref="arrow"
                    src="../../assets/pullDown.png"
                    style="position:absolute;width:18px;height:18px;right:9px;top:12px;cursor:pointer;pointer-events:none;transition-duration:0.3s"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-button
                  type="warning"
                  @click="search"
                >搜索</el-button>
                <el-button
                  class="reset-btn"
                  plain
                  @click="reset"
                >重置</el-button>
              </el-col>
            </el-row>
          </div>
        </el-form>
      </el-card>
      <div
        v-loading="listLoading"
        :class="{ 'loading-box': listLoading }"
        element-loading-text="加载中"
        style="margin-top: 20px"
      >
        <el-card
          class="table-card-box"
          shadow="never"
        >
          <el-button
            type="warning"
            class="customer-add-btn"
            @click="workArrangeSetting()"
          >排班设置</el-button>
          <el-button
            plain
            type="warning"
            class="customer-add-btn"
            @click="bindingWorkArrange()"
          >绑定排班</el-button>
          <el-table
            :data="workArrangeList"
            fit
            style="width: 100%"
            stripe
            :header-cell-style="{ background: 'rgba(250,252,255,1)' }"
            @selection-change="handleSelectionChange"
          >
            <div
              v-show="
                (!workArrangeList || workArrangeList.length <= 0) &&
                  !listLoading &&
                  !searchkey
              "
              slot="empty"
            >
              <img
                src="@/icons/pic-kong.png"
                alt="img"
                style="margin-top: 20px; width: 25%; height: 25%"
              />
              <p style="margin-top: -20px; padding-bottom: 0px">这里空空如也</p>
            </div>
            <el-card
              v-show="
                (!workArrangeList || workArrangeList.length <= 0) && !listLoading && searchkey
              "
              slot="empty"
              class="table-empty-box"
              shadow="never"
            >
              <empty></empty>
            </el-card>
            <el-table-column
              type="selection"
              fixed="left"
              width="60"
            >
            </el-table-column>
            <el-table-column
              align="left"
              fixed="left"
              label="员工账号"
              min-width="100"
              prop="employeeNumber"
            >
            </el-table-column>
            <el-table-column
              align="left"
              fixed="left"
              label="员工姓名"
              min-width="120"
              prop="name"
            >
            </el-table-column>
            <el-table-column
              align="left"
              fixed="left"
              label="员工电话"
              min-width="120"
              prop="phone"
            >
            </el-table-column>
            <el-table-column
              align="left"
              fixed="left"
              label="员工角色"
              min-width="100"
              prop="userType"
              :formatter="formatUserType"
            >
            </el-table-column>
            <el-table-column
              align="left"
              fixed="left"
              label="当前工作模式"
              min-width="110"
              prop="workPatternName"
            >
            </el-table-column>
            <el-table-column
              v-for="(item, index) in allDay"
              :key="index"
              :label="item + ''"
              align="left"
              width="80"
              prop="item + '"
              class-name="date"
            >
              <template v-slot="{ row }">
                <div
                  v-if="row[item]"
                  class="work"
                >上</div>
                <div
                  v-else
                  class="rest"
                >休</div>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <div class="pagination">
            <div class="pages">
              <el-pagination
                :current-page="searchFormData.page"
                :total="+total"
                :page-size="searchFormData.pageSize"
                :page-sizes="[10, 20, 30, 50]"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="sizeChange"
                @current-change="currentChange"
              />
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <WorkPatternBindingDialog
      ref="workPatternBindingDialog"
      class="add-form-dialog workArrange-manage-people-setting"
      :work-pattern-obj="workPatternObj"
      :user-id-list="userIdList"
      @updateWorkPatternList="getList"
    />
  </div>
</template>
<script>
import WorkPatternBindingDialog from './components/work-pattern-binding.vue'
import { agencyList } from '@/api/institutions'
import { workManage, selectWorkHistoryList } from '@/api/transit'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: {
    Treeselect,
    WorkPatternBindingDialog
  },
  data() {
    return {
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      },
      listLoading: false,
      searchFormData: {
        page: 1,
        pageSize: 10,
        agencyId: null,
        employeeNumber: '',
        month: '',
        name: '',
        workPatternId: null
      },
      total: 0,
      workArrangeList: [],
      allDay: 30,
      userIdList: [],
      workPatternList: [],
      workPatternObj: {},
      agencyOptions: [],
      searchkey: false
    }
  },
  created() {
    this.getList()
    this.agencyList()
    this.selectWorkHistoryList()
    const _this = this

    // 键盘enter操作
    document.onkeydown = function(e) {
      const key = window.event.keyCode
      if (key === 13) {
        _this.search()
      }
    }
  },
  methods: {
    async getList() {
      this.listLoading = true
      const { data } = await workManage(this.searchFormData)
      this.workArrangeList = data.items
      this.allDay = data.items[0]?.workSchedules.length
      data.items.forEach((item, index) => {
        item.workSchedules.forEach((el, i) => {
          item[i + 1] = el
        })
      })
      this.total = data.counts
      this.listLoading = false
    },
    async selectWorkHistoryList() {
      const { data } = await selectWorkHistoryList()
      this.workPatternObj = data
      for (const k in data) {
        this.workPatternList.push(...data[k])
      }
    },
    async agencyList() {
      const res = await agencyList()
      this.agencyOptions = JSON.parse(res.data)
    },
    selectAgency(val) {
      // 赋予搜索id为搜索框内的数据
      this.searchFormData.agentId = val.id
    },
    open() {
      // 将向下三角形，旋转为向右三角形
      this.$refs.arrow.style.transform = 'rotate(-180deg)'
    },
    close() {
      // 将向右三角形，旋转为向上三角形
      this.$refs.arrow.style.transform = 'rotate(0deg)'
    },
    formatUserType(row) {
      switch (row.userType) {
        case 1:
          return '员工'
        case 2:
          return '快递员'
        case 3:
          return '司机'
        default:
          return ''
      }
    },
    sizeChange(val) {
      this.searchFormData.pageSize = val
      this.getList()
    },
    currentChange(val) {
      this.searchFormData.page = val
      this.getList()
    },
    search() {
      this.searchFormData.page = 1
      this.searchkey = true
      this.getList()
    },
    reset() {
      this.searchFormData.agencyId = null
      this.searchFormData.employeeNumber = ''
      this.searchFormData.month = ''
      this.searchFormData.name = ''
      this.searchFormData.workPatternId = null
      this.searchkey = false
      this.getList()
    },
    workArrangeSetting() {
      this.$router.push('/transit/workArrange-setting')
    },
    handleSelectionChange(selection) {
      this.userIdList = []
      selection.forEach(item => {
        this.userIdList.push(item.id)
      })
    },
    bindingWorkArrange() {
      if (this.userIdList.length === 0) {
        this.$message.error('请选择调整人员')
        return
      }
      this.$refs.workPatternBindingDialog.openDialog()
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep th.date {
  .cell {
    padding-left: 17px !important;
  }
}
.work {
  width: 24px;
  height: 24px;
  background: #ffeeeb;
  border: 1px solid #e15536;
  border-radius: 50%;
  text-align: center;
  color: #e15536;
  font-size: 13px;
}
.workArrange-manage-people-setting {
  ::v-deep .el-dialog__body {
    padding: 30px 50px 10px 50px !important;
  }
  ::v-deep .el-dialog__footer {
    padding: 10px 20px;
    padding-bottom: 40px;
    .dialog-footer {
      .el-button {
        width: 89px;
        height: 36px;
        padding: 10px;
      }
      .el-button.is-plain{
        margin-left: 10px;
      }
    }
  }
}
.rest {
  width: 24px;
  height: 24px;
  background: #eff3f8;
  border: 1px solid #d8dde3;
  border-radius: 50%;
  text-align: center;
  color: #20232a;
  font-size: 13px;
}
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
}
.transport-task ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}
// 下拉框
::v-deep .el-select {
  width: 100%;
}
/*定义滚动条轨道 内阴影+圆角*/
::v-deep .el-table--scrollable-x .el-table__body-wrapper {
  padding-bottom: 5px;
  margin-bottom: 5px;
  &::-webkit-scrollbar {
    height: 10px;
  }

  /*定义滑块 内阴影+圆角*/
  &::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background-color: rgba(144, 147, 153, 0.3);
  }

  &::-webkit-scrollbar-track-piece {
    margin-right: 3px;
    margin-left: 3px;
  }
}
::v-deep .el-card {
  overflow: initial;
}
</style>
